export const BASE_URL = "https://api.baichuan-ai.com/v1"; // sp 
//正在请求的数量
let requestCount = 0
//显示loading
const showLoading = () => {
	if (requestCount === 0) {
		uni.showLoading({
			mask: true,
			title: '数据加载中'
		});
	}
	requestCount++;
}
//隐藏loading
const hideLoading = () => {
	requestCount--
	if (requestCount == 0) {
		uni.hideLoading();
	}
}

export const request = (options) => {
	return new Promise((resolve, reject) => {
		showLoading()
		uni.request({
			url: BASE_URL + options.url,
			method: options.methods || "GET",
			data: options.data || {},
			dataType: options.dataType || "json",
			header: {
				"Content-Type": "application/json",
				"Authorization": "Bearer sk-25292532803e381815bcdad577d3ddf5"
			},
			success: (res) => {
				if (res.statusCode == 200) {
					resolve(res)
				} else {
					return uni.showToast({
						icon: "none",
						title: "报错提示: " + res.data.error.message || "获取数据失败",
						duration: 6000
					})

				}
			},
			fail: (err) => {
				uni.showToast({
					icon: "none",
					title: "接口请求失败"
				})

				reject(err)
			},
			complete: () => {
				setTimeout(() => {
					hideLoading()
				}, 100)
			}
		})
	})
}